package com.ruoyi.api.controller.common;

import cn.hutool.core.lang.Validator;
import com.ruoyi.api.annotation.IgnoreAuth;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.picture.service.QiniuService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/common")
public class ApiCommonController {


	@Autowired
	private QiniuService qiniuService;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Autowired
	private ISysConfigService configService;

	/**
	 * 获取版本号
	 */
	@IgnoreAuth
	@GetMapping("/version")
	public AjaxResult version(){
		Map<String, Object> ajax = new HashMap<>(2);
		ajax.put("version", configService.selectConfigByKey("hy.apk.version"));
		ajax.put("apkDownloadUrl", configService.selectConfigByKey("hy.apk.downloadUrl"));
		return AjaxResult.success(ajax);
	}


	/**
	 * 通用上传请求
	 */
	@PostMapping("/upload")
	public AjaxResult uploadFile(MultipartFile file) throws Exception {
		try {
			//上传并返回新文件路径
			String url = qiniuService.uploadFile(file);
			Map<String, Object> ajax = new HashMap<>(1);
			ajax.put("url", url);
			return AjaxResult.success(ajax);
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}


	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/type/{dictType}")
	public AjaxResult dictType(@PathVariable String dictType)
	{
		List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
		if (Validator.isNull(data))
		{
			data = new ArrayList<SysDictData>();
		}
		return AjaxResult.success(data);
	}


}
